package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/session")
public class SessionServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("SessionServlet.service()");
		
		//HttpSession session1 = req.getSession();// 不加参数默认就是true。
		//HttpSession session2 = req.getSession(true);
		
		//HttpSession session3 = req.getSession(false);
		
		req.setAttribute("name", "张三");
		String name1 = (String) req.getAttribute("name");
		System.out.println(name1);
		
		HttpSession session = req.getSession();
		session.setAttribute("name", "李四");
		String name2 = (String) session.getAttribute("name");
		System.out.println(name2);
	}
	
}
